package org.scohen.juploadr.uploadapi.flickrrest.authentication;

import java.net.URL;
import java.util.Map;

import org.scohen.juploadr.uploadapi.Account;
import org.scohen.juploadr.uploadapi.CommunicationException;
import org.scohen.juploadr.uploadapi.ImageUploadApi;
import org.scohen.juploadr.uploadapi.ProtocolException;
import org.scohen.juploadr.uploadapi.flickrrest.RestFlickrApi;
import org.scohen.juploadr.uploadapi.flickrrest.upload.GetUploadStatus;
import org.scohen.juploadr.uploadapi.flickrrest.urls.GetUserPhotos;

public class FlickrAccount extends Account {

    private String password;
    private String frob;
    private String token;
    private boolean pro;
    private boolean unlimited;
    private static FlickrAccount nullAccount = new FlickrAccount();

    public static final FlickrAccount getNullAccount() {
        nullAccount = new FlickrAccount();
        
        return nullAccount;
    }

    public FlickrAccount() {
        super("null-account");
        this.username = "";
        this.pro = false;
    }

    public FlickrAccount(GetUploadStatus gus) {
        super(gus.getUsername());
        configureFrom(gus);
    }

    public FlickrAccount(String name) {
        super("account=" + System.currentTimeMillis());
    }

    public void configureFrom(GetUploadStatus gus) {
        this.username = gus.getUsername();
        this.name = gus.getUsername();
        this.setCurrentUploadUsed(gus.getUsedBandwidth());
        this.setMonthlyUploadLimit(gus.getMaxBandwidth());
        this.setUnlimited(gus.isUnlimited());
        this.setPro(gus.isPro());
        this.setUsername(gus.getUsername());
    }

    public String getPassword() {
        return password;
    }

    public boolean isPro() {
        return pro;
    }

    /**
     * @param password
     *            The password to set.
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @param pro
     *            The pro to set.
     */
    public void setPro(boolean pro) {
        this.pro = pro;
    }

    /**
     * @return Returns the frob.
     */
    public String getFrob() {
        return frob;
    }

    /**
     * @param frob
     *            The frob to set.
     */
    public void setFrob(String frob) {
        this.frob = frob;
    }

    /**
     * @return Returns the token.
     */
    public String getToken() {
        return token;
    }

    /**
     * @param token
     *            The token to set.
     */
    public void setToken(String token) {
        this.token = token;
    }

    public void save() {
        if (isAuthenticated() && getUsername() != null && getUsername().length() > 0) {
            super.save();
        }
    }

    public static Map<String,Account> allAccounts() {
        return allAccountsIn(FlickrAccount.class);
    }

    public URL getPhotoURL() {
        if (photoURL == null) {
            GetUserPhotos gup = new GetUserPhotos();
            try {
                gup.execute();
                photoURL = gup.getURL();

            } catch (ProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (CommunicationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return photoURL;
    }

    public ImageUploadApi getApi() {
        return RestFlickrApi.getInstance();
    }

    public boolean isUnlimited() {
        return unlimited;
    }

    public void setUnlimited(boolean unlimited) {
        this.unlimited = unlimited;
    }

}
